import React, { useEffect, useRef, useState } from 'react'
import useGetTableData from '@/hooks/useGetTableDataV2'
import SearchForm from '@/components/QsfCustomForm/SearchForm'
import { useForm } from 'antd/es/form/Form'
import PageContainer from '@/components/PageContainer'
import { useIndexColumns } from './TableColumns'
import { QuotationTrackVO as ITableData } from './types'
import CustomTable from '@/components/QsfCustomTable'
import { useNavigate } from 'react-router-dom'
import { useAppSelector } from '@/store/hooks'
import { searchConfig } from './FormConfig'
import useGetClassIds from '@/hooks/useGetClassIds'
import { getQuotationTrackList } from '@/api/monitor'
import useGetProDevSeason from '@/hooks/useGetProDevSeason'
import useSearchSupplier from '@/hooks/useSearchSupplier'
import './index.less'
import useCachePage from '@/hooks/useCachePage'
const Index: React.FC = () => {
  const navigate = useNavigate()
  const [form] = useForm()
  const ProDevSeason = useGetProDevSeason()
  const { SupplierOption, setSupplierOption, handleSearchSupplier } = useSearchSupplier('supplierId')
  const { setCachePage, getCachePage } = useCachePage()
  const buttonCodeList = useAppSelector((state) => state.login.buttonCodeList) || []
  const { classIdOptions, tinyClassIds, settinyClassIds, handleClassIdChange } = useGetClassIds()
  const tinyClassIdsRef = useRef<string[]>([])
  useEffect(() => {
    tinyClassIdsRef.current = tinyClassIds
  }, [tinyClassIds])
  const { tableProps, params, search, reset, reload } = useGetTableData<ITableData>(getQuotationTrackList, {
    form: form,
    immediatelySend: false,
    cachePageCallback: (current, size, prevSize) => {
      const cacheData = getCachePage()
      setCachePage({
        ...cacheData,
        current: prevSize === size ? current : 1,
        size,
      })
    },
    paramsCallback: (reqParams) => {
      return {
        ...reqParams,
        queryClassIdList: tinyClassIdsRef.current.length > 0 ? tinyClassIdsRef.current : null,
      }
    },
  })
  useEffect(() => {
    if (ProDevSeason && ProDevSeason.length > 0) {
      handleCacheData()
    }
  }, [ProDevSeason])
  const handleCacheData = async () => {
    const cacheData = getCachePage()
    if (cacheData == null) {
      const sortProDevSeason = ProDevSeason.sort((a: any, b: any) => b?.value - a?.value)
      const initParams = {
        seasonId: sortProDevSeason?.[0]?.value as number,
        orderTypes: [0],
      }
      form.setFieldsValue(initParams)
      await form.validateFields()
      search()
      setCachePage({ ...params, ...initParams })
    } else {
      form.resetFields()
      const { SupplierOption, tinyClassIds, ...resetCacheData } = cacheData
      if (tinyClassIds && tinyClassIds.length > 0) {
        settinyClassIds(tinyClassIds)
        tinyClassIdsRef.current = tinyClassIds
      }
      form.setFieldsValue({
        ...cacheData,
        queryClassIdList:
          cacheData.queryClassIdList && cacheData.queryClassIdList.length > 0 ? cacheData.queryClassIdList : null,
      })
      setSupplierOption(cacheData?.SupplierOption || [])
      reload({ ...resetCacheData })
    }
  }
  return (
    <PageContainer className="monitor_quotation_prog_wrap">
      <SearchForm
        form={form}
        searchConfig={searchConfig({
          SupplierOption,
          handleSearchSupplier,
          ProDevSeason,
          classIdOptions,
          settinyClassIds,
          handleClassIdChange,
        })}
        onFinish={async () => {
          setCachePage({
            ...form.getFieldsValue(),
            tinyClassIds: tinyClassIdsRef.current,
            SupplierOption,
            current: 1,
            size: params.size,
          })
          search()
        }}
        onReset={() => {
          settinyClassIds([])
          tinyClassIdsRef.current = []
          const sortProDevSeason = ProDevSeason.sort((a: any, b: any) => b?.value - a?.value)
          const initParams = {
            seasonId: sortProDevSeason?.[0]?.value as number,
            orderTypes: [0],
          }
          reset(initParams)
          form.setFieldsValue(initParams)
          setCachePage({ current: 1, size: 10, ...initParams })
        }}
      ></SearchForm>
      <CustomTable
        isAutoScrollY
        {...tableProps}
        columns={useIndexColumns({ navigate })}
        rowKey={(rows) => rows.supplierShortName + rows.bigClassName}
        // rowSelection={{
        //   selectedRowKeys,
        //   onChange(selectedRowKeys, selectedRows) {
        //     setselectedRowKeys(selectedRowKeys)
        //     setselectedRows(selectedRows)
        //   },
        // }}
      ></CustomTable>
    </PageContainer>
  )
}

export default Index
